const {updateNav,updateNavContent,queryNavList,saveNav,saveNavContent,deleteNav,queryById,deleteNavContent} = require('../service/nav.js')

class NavController {
	async create(ctx, next) {
		const {type,...object} = ctx.request.body
		if (type === 1){
		await saveNav(object)
			ctx.body = {
				code: 200,
				message: '导航创建成功',
			}
		}else {
			await saveNavContent(object)
			ctx.body = {
				code: 200,
				message: '导航内容创建成功',
			}
		}
	}

	async update(ctx, next) {
		const {type,...object} = ctx.request.body
		if (type === 1){
			await updateNav(object)
			ctx.body = {
				code: 200,
				message: '导航更新成功',
			}
		}else {
			await updateNavContent(object)
			ctx.body = {
				code: 200,
				message: '导航内容更新成功',
			}
		}
	}

	async deleteById(ctx, next) {
		const data = ctx.request.body
		if (Object.hasOwn(data,'children')){
			const res = await queryById(data.id)
			console.log(res)
			if (res.length){
				ctx.body = {
					message:'删除请先删除子节点'
				}
			}else {
				await deleteNav(data.id)
				ctx.body = {
					message:'删除成功'
				}
			}
		}else {
			await deleteNavContent(data.id)
			ctx.body = {
				message:'删除成功'
			}
		}
	}

	async query(ctx, next) {
		const data = await queryNavList()
		ctx.set({
			'Etag':'123'
		})
		ctx.body = {
			code: 200,
			message: '查询成功',
			data,
		}
	}
}



module.exports = new NavController()
